package utils.load
{
	import asds.map.HashMap;

	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;
	import flash.utils.Dictionary;

	public class SBitmapLoader extends Loader
	{
		private var _isCompleted:Boolean=false;
		public function SBitmapLoader()
		{
			super();
		}
		private var bitmapCache:HashMap=new HashMap();
		override public function load(request:URLRequest, context:LoaderContext=null):void{
			request.url=LoaderManager.urlFormat(request.url);
			if(bitmapCache.containsKey(request.url)){
				var l:SLoader=SLoader(bitmapCache.get(request.url))
				if(l.isCompleted){
					_loaderInfo=l.contentLoaderInfo;
					dispatchEvent(new Event(Event.COMPLETE));
				}else{
					l.contentLoaderInfo.addEventListener(Event.COMPLETE,onCompleted);
				}
				return
			}


			super.load(request,context);
			LoaderManager.cache.put(request.url,this);
			LoaderManager.addTask(this);
			_isCompleted=false;
			contentLoaderInfo.addEventListener(Event.COMPLETE,onCompleted);
		}

		private function onCompleted(e:Event):void{
			_isCompleted=true;
			_loaderInfo=LoaderInfo(e.currentTarget)
			e.currentTarget.removeEventListener(Event.COMPLETE,onCompleted);
			dispatchEvent(e);
		}
		private var _loaderInfo:LoaderInfo
		override public function get contentLoaderInfo():LoaderInfo{
			if(_loaderInfo) return _loaderInfo;
			return super.contentLoaderInfo;
		}
		/**
		 *
		 * @param url  URL字符串。
		 * @param context
		 *
		 */
		public function loadByUrl(url:String, context:LoaderContext=null):void{
			var u:URLRequest=new URLRequest(LoaderManager.urlFormat(url));
			super.load(u,context);
			LoaderManager.addTask(this);
			_isCompleted=false;
		}

		public function get isCompleted():Boolean
		{
			return _isCompleted;
		}


	}
}